<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NTP校时服务器管理</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .header p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .content {
            padding: 30px;
        }

        .status-card {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 25px;
            margin-bottom: 30px;
            border-left: 5px solid #007bff;
        }

        .status-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .status-item {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
            border: 1px solid #e9ecef;
        }

        .status-item h3 {
            color: #495057;
            margin-bottom: 10px;
            font-size: 1.1em;
        }

        .status-value {
            font-size: 1.3em;
            font-weight: bold;
            color: #2c3e50;
        }

        .status-value.running {
            color: #28a745;
        }

        .status-value.stopped {
            color: #dc3545;
        }

        .controls {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
            margin-bottom: 30px;
        }

        .btn {
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            font-size: 1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }

        .btn-primary {
            background: #007bff;
            color: white;
        }

        .btn-primary:hover {
            background: #0056b3;
            transform: translateY(-2px);
        }

        .btn-success {
            background: #28a745;
            color: white;
        }

        .btn-success:hover {
            background: #1e7e34;
            transform: translateY(-2px);
        }

        .btn-danger {
            background: #dc3545;
            color: white;
        }

        .btn-danger:hover {
            background: #c82333;
            transform: translateY(-2px);
        }

        .btn-warning {
            background: #ffc107;
            color: #212529;
        }

        .btn-warning:hover {
            background: #e0a800;
            transform: translateY(-2px);
        }

        .client-stats {
            background: white;
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.08);
        }

        .client-stats h2 {
            color: #2c3e50;
            margin-bottom: 20px;
            border-bottom: 2px solid #e9ecef;
            padding-bottom: 10px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
        }

        .stat-item {
            text-align: center;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 8px;
        }

        .stat-number {
            font-size: 2em;
            font-weight: bold;
            color: #007bff;
            display: block;
        }

        .stat-label {
            color: #6c757d;
            font-size: 0.9em;
            margin-top: 5px;
        }

        .loading {
            display: none;
            text-align: center;
            padding: 20px;
            color: #6c757d;
        }

        .error {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #f5c6cb;
        }

        .success {
            background: #d4edda;
            color: #155724;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 1px solid #c3e6cb;
        }

        @media (max-width: 768px) {
            .controls {
                flex-direction: column;
            }
            
            .btn {
                width: 100%;
            }
            
            .status-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🕐 NTP校时服务器</h1>
            <p>网络时间协议服务器管理与监控</p>
        </div>

        <div class="content">
            <div id="message"></div>
            
            <div class="controls">
                <button class="btn btn-success" onclick="startServer()">启动服务器</button>
                <button class="btn btn-danger" onclick="stopServer()">停止服务器</button>
                <button class="btn btn-warning" onclick="syncTime()">手动同步时间</button>
                <button class="btn btn-primary" onclick="refreshStatus()">刷新状态</button>
            </div>

            <div class="status-card">
                <h2>服务器状态</h2>
                <div class="status-grid">
                    <div class="status-item">
                        <h3>运行状态</h3>
                        <div id="running-status" class="status-value">检查中...</div>
                    </div>
                    <div class="status-item">
                        <h3>监听地址</h3>
                        <div id="host-port" class="status-value">-</div>
                    </div>
                    <div class="status-item">
                        <h3>当前时间</h3>
                        <div id="current-time" class="status-value">-</div>
                    </div>
                    <div class="status-item">
                        <h3>时间偏移</h3>
                        <div id="time-offset" class="status-value">-</div>
                    </div>
                    <div class="status-item">
                        <h3>最后同步</h3>
                        <div id="last-sync" class="status-value">-</div>
                    </div>
                </div>
            </div>

            <div class="client-stats">
                <h2>客户端统计</h2>
                <div class="stats-grid">
                    <div class="stat-item">
                        <span id="total-connections" class="stat-number">0</span>
                        <div class="stat-label">总连接数</div>
                    </div>
                    <div class="stat-item">
                        <span id="active-connections" class="stat-number">0</span>
                        <div class="stat-label">活跃连接</div>
                    </div>
                    <div class="stat-item">
                        <span id="last-client" class="stat-number">-</span>
                        <div class="stat-label">最后客户端</div>
                    </div>
                </div>
            </div>

            <div id="loading" class="loading">
                <p>正在加载...</p>
            </div>
        </div>
    </div>

    <script>
        // 自动刷新间隔（秒）
        const REFRESH_INTERVAL = 5;
        let refreshTimer = null;

        // 页面加载完成后开始自动刷新
        document.addEventListener('DOMContentLoaded', function() {
            refreshStatus();
            startAutoRefresh();
        });

        // 开始自动刷新
        function startAutoRefresh() {
            if (refreshTimer) {
                clearInterval(refreshTimer);
            }
            refreshTimer = setInterval(refreshStatus, REFRESH_INTERVAL * 1000);
        }

        // 停止自动刷新
        function stopAutoRefresh() {
            if (refreshTimer) {
                clearInterval(refreshTimer);
                refreshTimer = null;
            }
        }

        // 显示消息
        function showMessage(message, type = 'success') {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = `<div class="${type}">${message}</div>`;
            setTimeout(() => {
                messageDiv.innerHTML = '';
            }, 5000);
        }

        // 显示加载状态
        function showLoading(show = true) {
            document.getElementById('loading').style.display = show ? 'block' : 'none';
        }

        // 刷新状态
        async function refreshStatus() {
            try {
                showLoading(true);
                const response = await fetch('/api/status');
                const data = await response.json();

                if (data.error) {
                    showMessage(data.error, 'error');
                    return;
                }

                // 更新状态显示
                document.getElementById('running-status').textContent = data.running ? '运行中' : '已停止';
                document.getElementById('running-status').className = `status-value ${data.running ? 'running' : 'stopped'}`;
                
                document.getElementById('host-port').textContent = `${data.host}:${data.port}`;
                document.getElementById('current-time').textContent = data.current_time;
                document.getElementById('time-offset').textContent = `${data.time_offset} 秒`;
                document.getElementById('last-sync').textContent = data.last_sync_time;

                // 更新客户端统计
                document.getElementById('total-connections').textContent = data.client_stats.total_connections;
                document.getElementById('active-connections').textContent = data.client_stats.active_connections;
                
                if (data.client_stats.last_client_time) {
                    document.getElementById('last-client').textContent = '有连接';
                } else {
                    document.getElementById('last-client').textContent = '无连接';
                }

            } catch (error) {
                console.error('获取状态失败:', error);
                showMessage('获取状态失败: ' + error.message, 'error');
            } finally {
                showLoading(false);
            }
        }

        // 启动服务器
        async function startServer() {
            try {
                showLoading(true);
                const response = await fetch('/api/start', { method: 'POST' });
                const data = await response.json();

                if (data.error) {
                    showMessage(data.error, 'error');
                } else {
                    showMessage(data.message, 'success');
                    setTimeout(refreshStatus, 1000);
                }
            } catch (error) {
                console.error('启动服务器失败:', error);
                showMessage('启动服务器失败: ' + error.message, 'error');
            } finally {
                showLoading(false);
            }
        }

        // 停止服务器
        async function stopServer() {
            if (!confirm('确定要停止NTP服务器吗？')) {
                return;
            }

            try {
                showLoading(true);
                const response = await fetch('/api/stop', { method: 'POST' });
                const data = await response.json();

                if (data.error) {
                    showMessage(data.error, 'error');
                } else {
                    showMessage(data.message, 'success');
                    setTimeout(refreshStatus, 1000);
                }
            } catch (error) {
                console.error('停止服务器失败:', error);
                showMessage('停止服务器失败: ' + error.message, 'error');
            } finally {
                showLoading(false);
            }
        }

        // 手动同步时间
        async function syncTime() {
            try {
                showLoading(true);
                const response = await fetch('/api/sync', { method: 'POST' });
                const data = await response.json();

                if (data.error) {
                    showMessage(data.error, 'error');
                } else {
                    showMessage(data.message, data.success ? 'success' : 'error');
                    if (data.success) {
                        setTimeout(refreshStatus, 1000);
                    }
                }
            } catch (error) {
                console.error('同步时间失败:', error);
                showMessage('同步时间失败: ' + error.message, 'error');
            } finally {
                showLoading(false);
            }
        }

        // 页面卸载时停止自动刷新
        window.addEventListener('beforeunload', stopAutoRefresh);
    </script>
</body>
</html> 